package com.ssg1_8;

public class ArrayExe_5 {
    public static void main(String[] args) {
            //线性查找
       /* int [] arr={1,343,6,7,58,35,5};
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
        int num=35;
        boolean isFlag=true;
        for (int i = 0; i < arr.length; i++) {
        if (arr[i]==35){
            System.out.println("找到了指定元素，位置是："+i);
            isFlag=false;
        }
        }
    if (isFlag){
        System.out.println("很遗憾，没有找到啦！");

    }*/
        /*String  [] arr=new String[]{"jj","DD","HH","GGG","FFF","RR","KKK"};
        for (int i=0;i<arr.length;i++){
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
        String text="DD";
        boolean isFlag=true;
        for (int i = 0; i <arr.length ; i++) {
            if (text.equals(arr[i])){
                System.out.println("找到了指定的元素，位置是"+i);
                isFlag=false;
            }
        }
        if (isFlag){
            System.out.println("很遗憾没有找到啦！");
        }*/
        //二分查找，前提所要查找的数组必须有序
        int [] arr2=new int[]{1,-78,45,4,512,45,12,785,1245,454,412};
        int dext1=785;
        int head=0,end=arr2.length-1;
        boolean isFlag1=true;
        while(head<=end){
            int middle=(head+end)/2;
        if (arr2[middle]>dext1){
            end=middle-1;
        }else if (arr2[middle]<dext1){
            head=middle+1;
        }else {
            System.out.println("找到了要查找的元素，位置为："+middle);
                isFlag1=false;
                break;
        }

        }
        if (isFlag1){
            System.out.println("很遗憾，你没有找到！");
        }

    }
}
